package org.spring.springboot.app.sys.menu.controller;

import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.base.User;
import org.spring.springboot.app.sys.menu.domain.po.SysMenuPO;
import org.spring.springboot.app.sys.menu.domain.vo.SysMenuVO;
import org.spring.springboot.app.sys.menu.service.ISysMenuService;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(description="菜单操作接口",tags = ApiIndex.SysMenuController)
@RequestMapping(value = "/api/sysMenu")
@RestController
public class SysMenuController {

    @ApiOperation(value="查询用户菜单信息列表")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public R<List<SysMenuVO>> findSysMenuByUser(
            @ApiIgnore User user
    ) {
        R<List<SysMenuVO>> result = new R();
        String userId = user.getId();
        List<SysMenuPO> _list = sysMenuService.findSysMenuByUserId(userId);
        List<SysMenuVO> list = getSysMenuVOS(_list);
        result.setData(list);
        return result;
    }


    @Autowired
    private ISysMenuService sysMenuService;


    private List<SysMenuVO> getSysMenuVOS(List<SysMenuPO> _list) {
        List<SysMenuVO> list = new ArrayList<SysMenuVO>();
        if (_list != null && _list.size()>0) {
            for (SysMenuPO po : _list) {
                SysMenuVO vo = new SysMenuVO();
                BeanUtils.copyProperties(po,vo);
                list.add(vo);
            }
        }
        return list;
    }


    @ApiOperation(value="查询所有菜单信息列表")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public R<List<SysMenuVO>> findAll() {
        R<List<SysMenuVO>> result = new R();
        List<SysMenuPO> _list = sysMenuService.findAll();
        List<SysMenuVO> list = getSysMenuVOS(_list);
        result.setData(list);
        return result;
    }


    @ApiOperation(value="添加菜单信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(@ApiParam(value = "菜单对象", required = true) @RequestBody SysMenuVO sysMenuVO, BindingResult bindingResult) {
        R result = new R();
        if ( bindingResult.hasErrors() ) {
            result.setCode(1);
            result.setMessage(bindingResult.getAllErrors().get(0).toString());
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        SysMenuPO sysMenuPO = new SysMenuPO();
        BeanUtils.copyProperties(sysMenuVO,sysMenuPO);
        int code=sysMenuService.save(sysMenuPO);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("保存失败");
        }
        return result;
    }



    @ApiOperation(value="修改菜单信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public R update(@ApiParam(value = "菜单对象", required = true) @Valid  @RequestBody SysMenuVO sysMenuVO, BindingResult bindingResult) {
        R result = new R();
        if(  bindingResult.hasErrors() ) {
            result.setCode(1);
            result.setMessage("保存失败");
            result.setMessages(bindingResult.getAllErrors());
            return result;
        }
        SysMenuPO sysMenuPO = new SysMenuPO();
        BeanUtils.copyProperties(sysMenuVO,sysMenuPO);
        int code=sysMenuService.update(sysMenuPO);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("保存失败");
        }
        return result;
    }


    @ApiOperation(value="删除菜单信息")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R delete(@ApiParam(value = "菜单ID", required = true)  @PathVariable("id") String id) {
        R result = new R();
        if (StringUtils.isBlank(id)) {
            result.setType(Type.ParamValidateFail);
            result.setMessage("保存失败!菜单ID不能为空");
            return result;
        }
        int code= sysMenuService.delete(id);
        if (code == 0) {
            result.setCode(1);
            result.setMessage("保存失败");
        }
        return result;
    }

}
